﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Microsoft.Xna.Framework.Graphics;   //   for Texture2D
using Microsoft.Xna.Framework;  //  for Vector2

namespace zombieArena
{
    class Player : Character
    {
        public int score { get; set; }
        public Weapon weapon { get; set; }
        public Weapon altWeapon { get; set; }
        public Vector2 aimDirection { get; set; }
        public Vector2 moveDirection { get; set; }
        public bool invulnerable;
        public float invulnerableTimer;
        public bool hitSound;
        public bool hitSoundPlayed;

        public Player(int initX, int initY, int height, int width, Texture2D playerTexture)
        {
            // Initializing player
            this.position = new Vector2(initX, initY);
            this.lives = 3;
            this.velocity = new Vector2(15, 15);
            this.charTexture = playerTexture;
            this.height = height;
            this.width = width;
            this.directionAngle = 0;
            this.lookAngle = 0;
            currentSprite = 0;
            spriteRow = 2;
            this.state = "ALIVE";
            aimDirection = new Vector2(1, 0);
            moveDirection = new Vector2(0, 1);
            this.score = 0;

            hitColor = Color.White;

            invulnerable = false;
            hitSound = false;
            hitSoundPlayed = false;
            invulnerableTimer = 0f;
        }

        public Color hitColor;
        byte colorInterval = 16;
        int currentColor = 255;
        public void updateColor()
        {
            if (currentColor < 255)
            {
                hitColor.B = (byte)currentColor;
                hitColor.G = (byte)currentColor;
                currentColor += colorInterval;
            }
            if (currentColor >= 255)
            {
                currentColor = 255;
                hitColor.B = (byte)currentColor;
                hitColor.G = (byte)currentColor;
            }


        }

        public void changeHitColor()
        {
            hitColor.B = 0;
            hitColor.G = 0;
            currentColor = 0;
        }

        public void shoot(Map map, int type, Texture2D boom)
        {
            if(type == 0)
                weapon.shoot(position, aimDirection, map);
            else
                weapon.prototypes(position, aimDirection, map, type, boom);
        }

        public void altShoot(Map map, int type, Texture2D boom)
        {
            altWeapon.shoot(position, aimDirection, map);
        }

        public void updateInvulnerable(ref GameTime gameTime)
        {
            if (hitSoundPlayed)
            {
                hitSound = true;
            }
            if (this.invulnerable)
            {
                if (hitSoundPlayed)
                {
                    hitSound = false;
                    hitSoundPlayed = false;
                }
                invulnerableTimer += gameTime.ElapsedGameTime.Milliseconds;
            }
        }

        public void draw(ref SpriteBatch spriteBatch, ref Rectangle viewPort)
        {
            Vector2 playerscreenpos = position - new Vector2(viewPort.X, viewPort.Y);

            //spriteBatch.Draw(charTexture, playerscreenpos, new Rectangle(40 * currentSprite, 56 * spriteRow, width, height), Color.White);
            if (invulnerable)
            {
                if (invulnerableTimer > 3000f)
                {
                    invulnerable = false;
                    invulnerableTimer = 0f;
                }
                if(((int)(invulnerableTimer / 100f))%2 == 0)
                    spriteBatch.Draw(charTexture, playerscreenpos, new Rectangle(1 + (58 * currentSprite), 1 + (84 * spriteRow), width, height), hitColor, 0f, new Vector2(width / 2, height / 2), 1f, SpriteEffects.None, 0f);
            }
            else
                spriteBatch.Draw(charTexture, playerscreenpos, new Rectangle(1 + (58 * currentSprite), 1 + (84 * spriteRow), width, height), hitColor, 0f, new Vector2(width / 2, height / 2), 1f, SpriteEffects.None, 0f);
        }
    }
}
